package com.itwill.ecom.product;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository("productDao")
public class ProductDaoImpl implements ProductDao{
	public final static String namespace = "com.itwill.ecom.product";
	@Autowired
	private SqlSession session;

	public ProductDaoImpl() {
		System.out.println("#####ProductDao():디폴트 생성자");
	}

	
	
	public ProductDaoImpl(SqlSession session) {
		System.out.println("#####ProductDaoImpl(SqlSession session) 생성자");
		this.session = session;
	}



	public void setSession(SqlSession session) {
		System.out.println("###########setSession(SqlSession session)");
		this.session = session;
	}

	public SqlSession getSession() {
		System.out.println("########getSession()");
		return session;
	}

	// 상품 추가
	@Override
	public void create(ProductBeans product) throws Exception{
		session.insert(namespace + ".create", product);
	}

	// 상품 리스트로 찾기
	@Override
	public List<ProductBeans> getAll() throws Exception{
		return session.selectList(namespace + ".selectAllProduct");
	}

	// 상품번호으로 찾기
	@Override
	public ProductBeans getNo(int product_no) throws Exception{
		return session.selectOne(namespace + ".selectProductNo", product_no);
	}

	// 상품 제조사로 찾기
	@Override
	public List<ProductBeans> getCompany(int company) throws Exception{
		return session.selectList(namespace + ".selectProductListCompany",
				company);
	}

	// 상품 카테고리로 찾기
	@Override
	public List<ProductBeans> getCategory(int category) throws Exception{
		return session.selectList(namespace + ".selectProductListCategory",
				category);
	}

	// 상품 정보 수정
	@Override
	public int update(ProductBeans updateProduct) throws Exception{
		return session.delete(namespace + ".update", updateProduct);
	}

	// 상품 제거
	@Override
	public int delete(int product_no) throws Exception{
		return session.delete(namespace + ".delete", product_no);
	}

	/*// 상품상세 보기
	@Override
	public ProductBeans detail(int product_no) throws Exception{
		return session.selectOne(namespace+".detail", product_no);

	}*/

	@Override
	public int productAllCount() {
		return session.selectOne(namespace+".productAllCount");
	}

	@Override
	public int countByCompany(String company) throws Exception {
		return session.selectOne(namespace+".CountByCompany", company);
	}

	@Override
	public int countByCategory(String category) throws Exception {
		return session.selectOne(namespace+".CountByCategory", category);
	}
}
